package com.timemanager.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.timemanager.R;
import com.timemanager.db.TimeManagerDB;
import com.timemanager.model.Event;

public class AddEventActivity extends AppCompatActivity {

    private TextView titleText;
    private EditText eventNameEdit;
    private Button saveEvent;
    private String selectedType;
    private String getEditName;
    private Event event = new Event();
    private TimeManagerDB timeManagerDB;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        selectedType = getIntent().getStringExtra("selected_type");
        setContentView(R.layout.activity_add_event);
        eventNameEdit = (EditText) findViewById(R.id.event_name_edit);
        saveEvent = (Button) findViewById(R.id.save_event);
        titleText = (TextView) findViewById(R.id.title_text);
        titleText.setText("新增活动");
        timeManagerDB = TimeManagerDB.getInstance(this);
        event.setType(selectedType);
        saveEvent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!TextUtils.isEmpty(getEditName = eventNameEdit.getText().toString())) {
                    event.setName(getEditName);
                    if (timeManagerDB.saveEvent(event) > 0) {
                        showSucceedDialog();
                    } else {
                        showFailedDialog();
                    }
                }
                else {
                    Toast.makeText(AddEventActivity.this, "活动名称不能为空",
                            Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    /**
     * 保存成功弹出对话框
     * */
    private void showSucceedDialog() {
        AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(AddEventActivity.this);
        dialogBuilder.setTitle("保存成功");
        dialogBuilder.setMessage("是否继续添加常用活动？");
        dialogBuilder.setCancelable(false);
        dialogBuilder.setPositiveButton("继续", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Intent intent = new Intent(AddEventActivity.this, EventSelectorActivity.class);
                intent.putExtra("come_from", "add_event");
                startActivity(intent);
                finish();
            }
        });
        dialogBuilder.setNegativeButton("返回上级", (new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Intent intent = new Intent(AddEventActivity.this, MainActivity.class);
                startActivity(intent);
            }
        }));
        dialogBuilder.show();
        /*AlertDialog alertDialog = dialogBuilder.create();
        alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
        alertDialog.show();*/
    }

    /**
     * 保存失败弹出对话框
     * */
    private void showFailedDialog() {
        AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
        dialogBuilder.setTitle("保存失败");
        dialogBuilder.setMessage("该活动已经存在，无需添加。");
        dialogBuilder.setCancelable(false);
        dialogBuilder.setPositiveButton("重新输入", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
            }
        });
        dialogBuilder.setNegativeButton("返回上级", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                finish();
            }
        });
        /*AlertDialog alertDialog = dialogBuilder.create();
        alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
        alertDialog.show();*/
        dialogBuilder.show();
    }
}
